//todo 添加图片预览删除功能
$(".a-upload").on("change", "input", function () {
    var me = this;
    var ul = $(this).parent().parent().parent().parent()[0];
    var files = me.files;
    var existedImgLength = $(ul).find("img").length;
    var totalLength = files.length + existedImgLength;
    if (files.length > 5 || totalLength > 5) {
        prompt("最多可选5张图片");
        return;
    } else {
        var startIndex = 0;
        if (existedImgLength != 0) {
            startIndex = existedImgLength;
        }
        iterator(0);

        function iterator(i) {
            if (i == files.length) {
                if (totalLength == 5) {
                    $(ul).find("li.a-upload-box").css("display", "none");
                }
                return;
            }
            reader = new FileReader();
            reader.readAsDataURL(me.files[i]);
            reader.onload = function (e) {
                var data = e.target.result;
                //加载图片获取图片真实宽度和高度
                var image = new Image();
                image.onload = function () {
                    var width = image.width;
                    var height = image.height;
                };
                $.ajax({
                    url: "/index.php?m=Home&c=Order&a=ajaxUploadImg",
                    data: {'img': data},
                    type: "POST",
                    dataType: "json",
                    success: function (response) {
                        $(ul).find("li.a-upload-box").before("<li class ='ph'><input type = 'hidden'value='" + response.imgurl + "' ><img data-index='" + (startIndex + i) + "' src='" + data + "'/><i class='delete_img'>×</i></li>");
                        iterator(i + 1);
                    }
                })


            };
        }
    }
});

//todo 添加图片点击预览
$(".add_photo").on("click", "img", function () {
    var imgs = $(this).parent().parent().find("img");
    var _this = this;
    previewImg(imgs, _this);
});

function previewImg(imgs, _this) {
    var urls = [];
    for (var i = 0; i < imgs.length; i++) {
        urls.push(imgs[i].src);
        if (i == imgs.length - 1) {
            var current = _this.src;
            var obj = {
                urls: urls,
                current: current
            };
            previewImage.start(obj);
        }
    }
}

//todo 删除图片
$(".pic").on("click", ".delete_img", function () {
    $(this).parent()[0].remove();
});

//todo 点击录音
var timer;
var n ;
$(".make_record>div").on("touchstart", function (e) {
    n=1;
    $(".make_record>div").css("background-color", "#F8CFD9");
    $(".make_record>div>div").addClass("ripple");

    e.preventDefault();
    wx.startRecord({
        success: function () {
            localStorage.rainAllowRecord = 'true';
        },
        cancel: function () {
            alert('用户拒绝授权录音');
        }
    });
    var txt = $(this).parent().find('span.txt').html();

        $('.make_record span.txt').html("录音中");
        timer = setInterval(function () {
            n++;
            if(n===60){$(".make_record>div>div").removeClass("ripple");clearInterval(timer);$('.make_record span').html(n+"s″");}
        }, 1000)

});

$(".make_record>div").on('touchend', function (e) {
    e.preventDefault();
    clearInterval(timer);
    $(".make_record>div>div").removeClass("ripple");
    $(".make_record>div").css("background-color", "#f29fb3");
    $('.make_record span.txt').html(n + "s″");
    wx.stopRecord({
        success: function (res) {
            var localId = res.localId;
            $('#lid').val(localId);
            // uploadVoice();
        },
        fail: function (res) {
            alert(JSON.stringify(res));
        }
    });

});
$('#play').click(function () {
    var id = $('#lid').val();
    wx.playVoice({
        localId: id // 需要播放的音频的本地ID，由stopRecord接口获得
    });
});

//注册微信播放录音结束事件【一定要放在wx.ready函数内】
wx.onVoicePlayEnd({
    success: function (res) {
        var localId = res.localId;
        $('#lid').val(localId);
        stopWave();
    }
});



